package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.pack.PackBoxBean;
import com.scisoft.scan.data.pack.PackDetailBean;
import com.scisoft.scan.data.pack.PackInfoBean;
import com.scisoft.scan.data.pack.PackDetail04;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_Pack {

    public static  String GetWS_Url(){
        return String.format("http://%s/dymps/ws/SciPackServer?wsdl", SysParameter.WS_IPandPort);
    }

    public  static  String WS_Method = "receive";

    public  static  String WS_Namespace = "http://pack.scan.ws.dms.com/";

    //查询打包信息（bizId 1 未打包 2 打包中 3 打包）
    public static List<PackInfoBean> PACK_01(String bizId) throws Exception {
//        List<PackInfoBean> ret = new ArrayList<>();
//        PackInfoBean p = new PackInfoBean();
//        p.packId = "P11111111";
//        p.orderId = "D00001";
//        p.orderNo = "O000010203";
//        p.depotName = "J仓";
//        p.operator = "张三";
//        p.detail = "3项共18件";
//        p.optime = "2021年10月1日";
//        p.remark = "备注1111111";
//        ret.add(p);
//        return ret;

        //以下代码可以复用
        List<PackInfoBean> ret = new ArrayList<>();
        String arg0 = String.format("{\"flowNo\":\"PACK_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user ,SysParameter.ScSequence, bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(), new TypeToken<List<PackInfoBean>>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询一个客户订单明细中未打包的配件表
    public static List<PackDetailBean> PACK_02(String orderId) throws Exception {

//        List<PackDetailBean> ret = new ArrayList<>();
//        PackDetailBean p = new PackDetailBean();
//        p.partId = "P001";
//        p.partNo = "P001";
//        p.partName = "方向盘";
//        p.manufacturer = "G01";
//        p.manufacturerName = "大运汽配";
//        p.totalCount = "50";
//        p.packedCount = "20";
//        p.notpackCount = "30";
//        p.orderNo = "O0125368";
//        p.optime = "2021年10月1日";
//        ret.add(p);
//        return ret;
        //以下代码可以复用
        String arg0=String.format( "{\"flowNo\":\"PACK_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        List<PackDetailBean> ret = new ArrayList<>();
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");

        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret= gson.fromJson(jsonArray.toString(),new TypeToken<List<PackDetailBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询一个客户的已打包的箱号和配件信息（bizId 1 未打包 2 打包中 3 打包）
    public static List<PackBoxBean> PACK_03(String orderId,String orderType) throws Exception {

//        List<PackBoxBean> ret = new ArrayList<>();
//        PackBoxBean packBoxBean = new PackBoxBean();
//        packBoxBean.packingNo = "1";
//        packBoxBean.packingId = "1";
//        PackDetailBean p = new PackDetailBean();
//        p.partId = "P001";
//        p.partNo = "P001";
//        p.partName = "方向盘";
//        p.manufacturer = "G01";
//        p.manufacturerName = "大运汽配";
//        p.totalCount = "50";
//        p.packedCount = "20";
//        p.notpackCount = "30";
//        p.orderNo = "O0125368";
//        p.optime = "2021年10月1日";
//        packBoxBean.packDetailBeanList.add(p);
//        ret.add(packBoxBean);
//        return ret;
        String bizId= orderId+","+orderType;
        String arg0 = String.format( "{\"flowNo\":\"PACK_03\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        List<PackBoxBean> ret = new ArrayList<>();
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");

        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret= gson.fromJson(jsonArray.toString(),new TypeToken<List<PackBoxBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //打包某个配件到某个箱号内
    public static String PACK_04(PackBoxBean packBoxBean,String packCount,String dtlId,String orderId)throws Exception{

//        return "成功";
        //以下代码可以复用
        PackDetail04 ret=new PackDetail04(packBoxBean.packingId,packBoxBean.packingNo,dtlId,packCount,orderId);
        Gson gson = new Gson();
        String parm =gson.toJson(ret);
        String arg0 = String.format( "{\"flowNo\":\"PACK_04\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //全部打包完成
    public static String PACK_05(String orderId)throws Exception {

//        return "成功";
        //以下代码可以复用
        String arg0 = String.format( "{\"flowNo\":\"PACK_05\",\"account\":\"%s\",\"bizId\":%s,\"scSequence\":\"%s\"}",SysParameter.user,orderId, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //删除打包信息
    public static String PACK_06(String bizId)throws Exception {
//        return "成功";
        //以下代码可以复用
        String arg0 = String.format( "{\"flowNo\":\"PACK_06\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {

        }else
        {
            ret =msg;
            throw new Exception(msg);
        }
        return ret;
    }

    //按照明细删除 （已装箱明细ID）
    public static String PACK_07(String bizId)throws Exception{
//        return "成功";
        //以下代码可以复用
        String arg0 = String.format( "{\"flowNo\":\"PACK_07\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {

        }else
        {
            ret =msg;
            throw new Exception(msg);
        }
        return ret;
    }

    public static  SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope,headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
